package com.ds.lens.data.service.dao;

import com.ds.lens.data.service.dao.entity.LensMonitorStateInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface LensMonitorStateInfoDao {

    String COLUMNS = " id, monitor_id, version, state, pre_state, target, created_time ";

    @Insert({
            "insert into lens_monitor_state_info (monitor_id, version, ",
            "state, pre_state, target,created_time )",
            "values (#{monitorId,jdbcType=BIGINT}, #{version,jdbcType=INTEGER}, ",
            "#{state,jdbcType=SMALLINT}, #{preState,jdbcType=SMALLINT}, #{target,jdbcType=VARCHAR}, #{createdTime,jdbcType=TIMESTAMP})"
    })
    int insert(LensMonitorStateInfo record);


    @Insert({
            "<script>",
            "insert into lens_monitor_state_info (monitor_id, version, ",
            "state, pre_state, target,created_time )",
            "values",
            "<foreach collection='records' item='record' separator=','>",
            "(#{record.monitorId}, #{record.version}, #{record.state}, #{record.preState},#{record.target},#{record.createdTime})",
            "</foreach>",
            "</script>"
    })
    int batchInsert(@Param("records") List<LensMonitorStateInfo> records);

    @Select({
            "select",
            COLUMNS,
            "from lens_monitor_state_info",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "monitorState", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "monitor_id", property = "monitorId", jdbcType = JdbcType.BIGINT),
            @Result(column = "version", property = "version", jdbcType = JdbcType.INTEGER),
            @Result(column = "state", property = "state", jdbcType = JdbcType.SMALLINT),
            @Result(column = "pre_state", property = "preState", jdbcType = JdbcType.SMALLINT),
            @Result(column = "target", property = "target", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP)
    })
    LensMonitorStateInfo selectByPrimaryKey(Long id);

    @Select({
            "<script>",
            "select ",
            COLUMNS,
            " from lens_monitor_state_info",
            " where monitor_id = #{monitorId} and version = #{version} ",
            "<if test='target != null'> and target = #{target} </if>",
            "<if test='states != null and states.size > 0'> and state in ",
            "<foreach collection='states' item='state' separator=',' open='(' close=')' >",
            "#{state}",
            "</foreach>",
            " </if>",
            "<if test='startTime != null and endTime != null'> and created_time between #{startTime} and #{endTime} </if>",
            " order by created_time asc ",
            "</script>"
    })
    @ResultMap("monitorState")
    List<LensMonitorStateInfo> selectByMonitor(@Param("monitorId") Long monitorId,
                                               @Param("version") Integer version,
                                               @Param("target") String target,
                                               @Param("states") List<Integer> states,
                                               @Param("startTime") Date startTime,
                                               @Param("endTime") Date endTime);

}
